﻿using BCCommon;
using BCEntity.TMS.PlatformManageFee.PlatformManageFeeConfig;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using BCCommon.TMS.ManageFeeBusiness;
using System.Data;

namespace BCData.TMS.PlatformManageFee.PlatformManageFeeConfig
{
    public class PlatformManageFeeConfigData : IPlatformManageFeeConfigData
    {
        private readonly IDatabaseContext database;

        private const string QUERY = @"SELECT PlatformManageFeeConfigId,AgentRecordId,CompanyId,SubAgentRecordId,SubCompanyId,ManageFeeBusinessType,ManageFeeRatio,
                                        CurrentlyPlatformManageFeeExecutionPlanId,CreateTime,CreateAdminId,CreateAdminName 
                                        FROM PlatformManageFeeConfig";

        private const string QUERY_JOIN = @"SELECT A.PlatformManageFeeConfigId,A.AgentRecordId,A.CompanyId,A.SubAgentRecordId,A.SubCompanyId,A.ManageFeeBusinessType,A.ManageFeeRatio,
                                        A.CurrentlyPlatformManageFeeExecutionPlanId,A.CreateTime,A.CreateAdminId,A.CreateAdminName,B.CompanyName AS SubCompanyName,C.CompanyName   
                                        FROM PlatformManageFeeConfig A
                                        INNER JOIN Company B ON A.SubCompanyId = B.CompanyId 
                                        LEFT JOIN Company C ON A.CompanyId = C.CompanyId";

        public PlatformManageFeeConfigData(IDatabaseContext database)
        {
            this.database = database;
        }

        public async Task<PlatformManageFeeConfigEntity> GetEntity(long platformManageFeeConfigId)
        {
            return await database.Slave.QueryEntityAsync<PlatformManageFeeConfigEntity>($"{QUERY} WHERE PlatformManageFeeConfigId = ?PlatformManageFeeConfigId", new { platformManageFeeConfigId });
        }

        public async Task<int> Delete(long platformManageFeeConfigId)
        {
            return await database.Master.ExecuteAsync("DELETE FROM PlatformManageFeeConfig WHERE PlatformManageFeeConfigId = ?PlatformManageFeeConfigId", new { platformManageFeeConfigId });
        }

        public async Task<(IEnumerable<PlatformManageFeeConfigViewEntity>, int)> GetList(long? logonCompanyId, int? companyId, TMSManageFeeBusinessType? manageFeeBusinessType, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (logonCompanyId.HasValue)
            {
                wheres.Add("A.SubCompanyId = ?SubCompanyId");
                parameters.Add("?SubCompanyId", logonCompanyId, DbType.Int32);
            }
            if (companyId.HasValue)
            {
                wheres.Add("A.CompanyId = ?CompanyId");
                parameters.Add("?CompanyId", logonCompanyId, DbType.Int32);
            }
            if (manageFeeBusinessType.HasValue)
            {
                wheres.Add("A.ManageFeeBusinessType = ?ManageFeeBusinessType");
                parameters.Add("?ManageFeeBusinessType", manageFeeBusinessType.Value.ToString(), DbType.AnsiString);
            }
            string sql = string.Format(@"{0} {1} ORDER BY A.PlatformManageFeeConfigId DESC LIMIT {2},{3};
                            SELECT COUNT(1) FROM PlatformManageFeeConfig A {1}",
                            QUERY_JOIN,
                            wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty,
                            (pageIndex - 1) * pageSize,
                            pageSize);
            return await database.Slave.QueryPageableListAsync<PlatformManageFeeConfigViewEntity>(sql, parameters);
        }

        public async Task<int> UpdateCurrentlyPlanId(long platformManageFeeExecutionPlanId, long subAgentRecordId, TMSManageFeeBusinessType manageFeeBusinessType, decimal manageFeeRatio)
        {
            DynamicParameters parameters = new DynamicParameters();
            string sql = @"UPDATE PlatformManageFeeConfig 
                            SET ManageFeeRatio = ?ManageFeeRatio,
                            CurrentlyPlatformManageFeeExecutionPlanId = ?CurrentlyPlatformManageFeeExecutionPlanId 
                            WHERE SubAgentRecordId = ?SubAgentRecordId
                            AND ManageFeeBusinessType = ?ManageFeeBusinessType";
            parameters.Add("?ManageFeeRatio", manageFeeRatio, DbType.Decimal);
            parameters.Add("?CurrentlyPlatformManageFeeExecutionPlanId", platformManageFeeExecutionPlanId, DbType.Int64);
            parameters.Add("?SubAgentRecordId", subAgentRecordId, DbType.Int64);
            parameters.Add("?ManageFeeBusinessType", manageFeeBusinessType.ToString(), DbType.AnsiString);
            return await database.Master.ExecuteAsync(sql, parameters, database.Transaction);
        }
    }
}
